package ds.factcom.view.ui.client;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ds.factcom.controller.client.ClientController;
import ds.factcom.domain.client.Client;
import ds.factcom.domain.client.DocumentType;
import ds.factcom.exception.EmptyStringException;
import ds.factcom.exception.ExistDomainException;
import ds.factcom.exception.IsNotNumberException;
import ds.factcom.exception.IsNotStringException;
import ds.factcom.exception.NotCorrectDocumentNumberException;
import ds.factcom.exception.NotCorrectDocumentTypeException;
import ds.factcom.exception.NotExistDomainException;
import ds.factcom.view.ui.Messages;
import ds.factcom.view.ui.StateDomain;
import ds.factcom.view.validate.ValidatorClient;

@SuppressWarnings("serial")
public class ClientView extends JDialog {
	
	private JPanel textPanel;
	private JLabel nameLabel;
	private JLabel addressLabel;
	private JLabel documentTypeLabel;
	private JLabel documentNumberLabel;
	private JTextField nameTF;
	private JTextField  addressTF;
	private JTextField documentNumberTF;
	private JComboBox documentTypeCB;
	
	private JPanel buttonsPanel;
	private JButton acceptBtn; 
	private JButton cancelBtn;
	private JButton searchBtn;
	
	private ClientController clientController;
	private StateDomain title;
	
	public ClientView(ClientController clientController, StateDomain stateDomain){
		this.clientController = clientController;
		this.title = stateDomain;
		initGUI();
	}

	private void initGUI() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLayout(new BorderLayout());
		setLocation(350, 300);
		setSize(500, 150);
		setResizable(false);
		setTitle(title + "Cliente");
		setModal(true);
		add(getTextPanel(), BorderLayout.CENTER);
		add(getButtonsPanel(), BorderLayout.SOUTH);
		enabledButtons(title);
		enabledTextField(title);
	}
		
//*************************  Panel de Campos ************************************++
	
	private JPanel getTextPanel() {
		if (textPanel == null) {
			textPanel = new JPanel(new GridLayout(4,2));
			textPanel.add(getNameLabel());
			textPanel.add(getNameTF());
			textPanel.add(getDocumentTypeLabel());
			textPanel.add(getDocumentTypeCB());
			textPanel.add(getDocumentNumberLabel());
			textPanel.add(getDocumentNumberTF());
			textPanel.add(getAddressLabel());
			textPanel.add(getAddressTF());
		}
		return textPanel;
	}

	private JLabel getNameLabel() {
		if (nameLabel == null) {
			nameLabel = new JLabel("Nombre");
		}
		return nameLabel;
	}

	private JLabel getAddressLabel() {
		if (addressLabel == null) {
			addressLabel = new JLabel("Direccion");
		}
		return addressLabel;
	}

	private JLabel getDocumentTypeLabel() {
		if (documentTypeLabel == null) {
			documentTypeLabel = new JLabel("Tipo de Documento");
		}
		return documentTypeLabel;
	}

	private JLabel getDocumentNumberLabel() {
		if (documentNumberLabel == null) {
			documentNumberLabel = new JLabel("Numero de Documento");
		}
		return documentNumberLabel;
	}

	private JTextField getNameTF() {
		if (nameTF == null) {
			nameTF = new JTextField();
		}
		return nameTF;
	}

	private JTextField getAddressTF() {
		if (addressTF == null) {
			addressTF = new JTextField();
		}
		return addressTF;
	}

	private JTextField getDocumentNumberTF() {
		if (documentNumberTF == null) {
			documentNumberTF = new JTextField();
		}
		return documentNumberTF;
	}

	private JComboBox getDocumentTypeCB() {
		if (documentTypeCB == null) {
			documentTypeCB = new JComboBox();
			DefaultComboBoxModel comboBoxModel = new DefaultComboBoxModel();
			comboBoxModel.addElement(DocumentType.CUIT);
			comboBoxModel.addElement(DocumentType.DU);
			comboBoxModel.addElement(DocumentType.DNI);
			documentTypeCB.setModel(comboBoxModel);
		}
		return documentTypeCB;
	}

//*************************  Botonera ************************************++
	private JPanel getButtonsPanel() {
		if (buttonsPanel == null) {
			buttonsPanel = new JPanel(new GridLayout(1,3));
			buttonsPanel.add(getSearchBtn());
			buttonsPanel.add(getAcceptBtn());
			buttonsPanel.add(getCancelBtn());
		}
		return buttonsPanel;
	}

	private JButton getAcceptBtn() {
		if (acceptBtn == null) {
			acceptBtn = new JButton("Guardar");
			acceptBtn.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					try {
						String name = getNameTF().getText();
						String documentType = getDocumentTypeCB().getSelectedItem().toString();
						String documentNumber = getDocumentNumberTF().getText();
						ValidatorClient.isNotEmpty(name);
						ValidatorClient.isCorrectDocumentType(documentType);
						ValidatorClient.isCorrectDocumentNumber(documentNumber);

						if (clientController.getClient() != null) {
							clientController.modify(name, getAddressTF().getText());
						} else {
							clientController.insert(name, documentType, documentNumber, getAddressTF().getText());
						}
						clientController.setClient(null);
						Messages.showInformationDialog(ClientView.this, "El cliente se ha guardado correctamente", "Informacion Cliente");
						if (Messages.showConfirmDialog(ClientView.this, "Desea " + title+ " otro cliente ?", "Informacion Cliente") 
								== JOptionPane.NO_OPTION) {
							ClientView.this.dispose();
						} else {
							enabledButtons(title);
							cleanField();
						}
					} catch (ExistDomainException e1) {
						Messages.showErrorMessage(ClientView.this, e1.getMessage());
					} catch (EmptyStringException e1) {
						Messages.showErrorMessage(ClientView.this, e1.getMessage());
					} catch (IsNotStringException e1) {
						Messages.showErrorMessage(ClientView.this, e1.getMessage());
					} catch (NotCorrectDocumentTypeException e1) {
						Messages.showErrorMessage(ClientView.this, e1.getMessage());
					} catch (IsNotNumberException e1) {
						Messages.showErrorMessage(ClientView.this, e1.getMessage());
					} catch (NotCorrectDocumentNumberException e1) {
						Messages.showErrorMessage(ClientView.this, e1.getMessage());
					} catch (NotExistDomainException e1) {
						Messages.showErrorMessage(ClientView.this, e1.getMessage());
					}
				}
			});
		}
		return acceptBtn;
	}

	private JButton getCancelBtn() {
		if (cancelBtn == null) {
			cancelBtn = new JButton("Cancelar");
			cancelBtn.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					cleanField();
					enabledButtons(title);
				}
			});
		}
		return cancelBtn;
	}
	
	private JButton getSearchBtn() {
		if (searchBtn == null) {
			searchBtn = new JButton("Buscar");
			searchBtn.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					new SearchClientView(clientController).open();
					cleanField();
					enabledButtons(true);
					ClientView.this.repaint();
				}
			});
		}
		return searchBtn;
	}

	private void cleanField() {
		Client client = clientController.getClient();
		if (client != null) {
			getNameTF().setText(client.getName());
			getAddressTF().setText(client.getAddress());
			getDocumentNumberTF().setText(client.getDocument().getNumber());
			getDocumentTypeCB().setSelectedItem(client.getDocument().getDocumentType());
		} else {
			getNameTF().setText("");
			getAddressTF().setText("");
			getDocumentNumberTF().setText("");
			getDocumentTypeCB().setSelectedIndex(0);
		}
		enabledTextField(title);
	}

	private void enabledTextField(StateDomain stateDomain) {
		if (stateDomain.equals(StateDomain.INSERT)) {
			getDocumentNumberTF().setEnabled(true);
			getDocumentTypeCB().setEnabled(true);
		} else if (stateDomain.equals(StateDomain.MODIFY)) {
			getDocumentNumberTF().setEnabled(false);
			getDocumentTypeCB().setEnabled(false);
		}
	}
	
	private void enabledButtons(StateDomain stateDomain) {
		if (stateDomain.equals(StateDomain.INSERT)) {
			enabledButtons(true);
		} else if (stateDomain.equals(StateDomain.MODIFY)) {
			enabledButtons(false);
		}
	}
	
	private void enabledButtons(boolean enabled) {
		getAcceptBtn().setEnabled(enabled);
		getCancelBtn().setEnabled(enabled);
		getSearchBtn().setEnabled(!enabled);
	}
	
	public void open() {
		setVisible(true);
	}
}